package jdbcmapper;

import lotus.domino.*;
import java.util.*;


/**
 * <p>Title: JDBC Mapper</p>
 *
 * <p>Description: Libraries to copy documents form Lotus Notes to jdbc
 * table</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: The Salvation ARmy</p>
 *
 * @author Ansel Robateau
 * @version 1.1
 */
public class NotesDB extends NotesDoc{
    public static final String NOTESDATABASE = "NotesDatabase";
    public static final String SERVER = "Server";
    public static final String FILENAME = "FileName";
    public static final String date_LASTACCESSED = "LastAccessed";
    private Mapping m_parent;
    public NotesDB(Session session,Document doc) {
        super(session,doc);
    }
    public NotesForm getFirstForm(){
        return (NotesForm)this.getFirst();
    }
    public NotesForm getNextForm(){
        return (NotesForm)this.getNext();
    }
    public Mapping getParent(){
        return this.m_parent;
    }
    /**
     * LoadMapping
     */
    public void LoadMapping(Mapping parent) throws Exception {
        this.m_parent = parent;
        Hashtable forms = new Hashtable();
        //View view = this.getDocument().getParentDatabase().getView("(FieldMaps by Notes Database)");
        //ViewEntryCollection vc = view.getAllEntriesByKey(this.getString(NotesDB.NOTESDATABASE));
        String str = "Form = \"Field Map\" & NotesDatabase = \""+
                                 this.getString(this.NOTESDATABASE)+"\"";
        //System.out.println(str);
        this.getM_log().add(str);
        DocumentCollection col = this.getDocument().getParentDatabase().search(str);
        //System.out.println("Documents found = " + col.getCount());
        this.getM_log().add("Documents found = " + col.getCount());
        //ViewEntry entry = vc.getFirstEntry();
        Document doc = col.getFirstDocument();
        while(doc != null){
            //doc = entry.getDocument();
            FieldMap map = new FieldMap(this.getSession(), doc);
            String form_name = map.getString(FieldMap.NOTESFORM);
            String table_name = map.getString(FieldMap.SQLTABLE);
            try {
                if(forms.get(form_name+"~"+table_name) == null){
                    throw new Exception();
                }
            }catch (Exception e) {
                //View view1 = this.getDocument().getParentDatabase().getView("(Forms by SQL Database)");
                Vector keys = new Vector();
                keys.add(map.getString(FieldMap.SQLDATABASE));
                keys.add(map.getString(FieldMap.SQLTABLE));
                keys.add(map.getString(FieldMap.NOTESDATABASE));
                keys.add(map.getString(FieldMap.NOTESFORM));
                //ViewEntryCollection vc1 = view1.getAllEntriesByKey(keys);
                //ViewEntry entry1 = vc1.getFirstEntry();
                String searchstr = "Form = \"Notes Form\" & "+map.SQLDATABASE+
                                   " = \"" + map.getString(map.SQLDATABASE) + "\" & "+
                                   map.SQLTABLE + " = \"" + map.getString(map.SQLTABLE) +
                                   "\" & " + map.NOTESDATABASE + " = \"" +
                                   map.getString(map.NOTESDATABASE) + "\" & " +
                                  map.NOTESFORM + " = \"" + map.getString(map.NOTESFORM) +
                                   "\"";
                //System.out.println(searchstr);
                DocumentCollection notesforms = this.getDocument().getParentDatabase().search(searchstr);
                //System.out.println("Documents found = " + notesforms.getCount());
                NotesForm form = null;
                if(notesforms.getCount() == 0){
                    Document formdoc = this.getDocument().getParentDatabase().createDocument();
                    formdoc.appendItemValue("Form","Notes Form");
                    formdoc.appendItemValue(NotesForm.NOTESDATABASE,map.getString(FieldMap.NOTESDATABASE));
                    formdoc.appendItemValue(FieldMap.NOTESFORM,map.getString(FieldMap.NOTESFORM));
                    formdoc.appendItemValue(FieldMap.SQLDATABASE,map.getString(FieldMap.SQLDATABASE));
                    formdoc.appendItemValue(FieldMap.SQLTABLE,map.getString(FieldMap.SQLTABLE));
                    formdoc.save();
                    form = new NotesForm(this.getSession(),formdoc);
                }else{
                    Document formdoc = notesforms.getFirstDocument();
                    form = new NotesForm(this.getSession(),formdoc);
                }
                this.m_subs.add(form);
                form.LoadMapping(this,map);
                forms.put(form_name+"~"+table_name,"");
            }
            //entry = vc.getNextEntry();
            doc = col.getNextDocument(doc);
        }
    }

    public Vector getM_log() {
        return this.getParent().getM_log();
    }
}
